﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace yule.DAL
{
    /// <summary>
    /// 数据库访问抽象基类
    /// </summary>
    public abstract class BaseDAL<TModel> :
        yule.Common.WCF.WcfService, yule.IDAL.IBaseDAL<TModel> where TModel : class, yule.Model.IModel<TModel>, new()
    {

        protected abstract TModel SelectFromDB(dynamic id);

        public TModel Select(dynamic id)
        {
            //先读取缓存；在操作数据库
            TModel tmodel = this.GetCache<TModel>(id);
            if (tmodel == null)
            {
                tmodel = this.SelectFromDB(id);
            }
            return tmodel;
        }

        protected yule.Data.IDBHelper DBHelper
        {
            get
            {
                return yule.Data.DBHelper.Instance;
            }
        }

        protected IList<T> ConverToList<T>(System.Data.DataTable dt) where T : class, yule.Model.IModel<T>, new()
        {
            return this.ConverToList<T>(dt, null);
        }

        protected IList<T> ConverToList<T>(System.Data.DataTable dt, System.Action<T> action) where T : class, yule.Model.IModel<T>, new()
        {
            IList<T> list = new List<T>();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    T tmodel = new T();
                    tmodel.Convert(row);
                    list.Add(tmodel);
                    if (action != null)
                    {
                        action(tmodel);
                    }
                }
            }
            return list;
        }

        protected void RemoveCache<T>(T model) where T : class, yule.Model.IModel<T>, new()
        {
            if (IsUseCache<T>() == true && model != null)
            {
                bool b = yule.Common.CacheManager.Cache.Remove<T>(model.Id);
            }
        }

        protected void RemoveCache<T>(dynamic id) where T : class, yule.Model.IModel<T>, new()
        {
            if (IsUseCache<T>() == true && yule.Util.Verificate.IsNull(id) == false)
            {
                bool b = yule.Common.CacheManager.Cache.Remove<T>(id);
            }
        }

        protected void SetCache<T>(T model) where T : class, yule.Model.IModel<T>, new()
        {
            if (IsUseCache<T>() == true && model != null)
            {
                bool b = yule.Common.CacheManager.Cache.Set<T>(model.Id, model);
            }
        }

        private T GetCache<T>(dynamic id) where T : class, yule.Model.IModel<T>, new()
        {
            T t = null;
            if (IsUseCache<T>() == true)
            {
                t = id == null ? null : yule.Common.CacheManager.Cache.Get<T>(id);
            }
            return t;
        }

        /// <summary>
        /// 判断该类型是否使用了缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private static bool IsUseCache<T>() where T : class, yule.Model.IModel<T>, new()
        {
            return true;
        }

    }
}